package com.dbtools;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.avecsys.quotemanager.constants.BusinessCodes;
import com.avecsys.quotemanager.dao.common.BaseDAO;
import com.avecsys.quotemanager.pojos.menu.RootMenuEntity;
import com.avecsys.quotemanager.pojos.menu.SubMenuEntity;

public class BuildMenu
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(
				"applicationContext.xml");
		buildMenuSystem(applicationContext);
	}

	public static void buildMenuSystem(ApplicationContext applicationContext)
	{
		BaseDAO baseDAO = (BaseDAO) applicationContext.getBean("baseDAO");
		Session session = baseDAO.getHibernateTemplate().getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		// Remove Existing Menus
		removeExistingMenus(session);
		createNewMenus(session);
		tx.commit();
	}

	private static void createNewMenus(Session session)
	{
		// Root Menus
		// System Admin Root Menus
		RootMenuEntity rootMenuSupplierMaster = new RootMenuEntity("RM_SUPPLIER_MASTER",
				"Supplier Master", BusinessCodes.MENU_TYPE_SYSTEM_ADMIN, 0L);
		RootMenuEntity rootMenuSuperUserMaster = new RootMenuEntity("RM_SUPER_USER_MASTER",
				"Super User Master", BusinessCodes.MENU_TYPE_SYSTEM_ADMIN, 0L);
		// Supplier Root Menus
		RootMenuEntity rootMenuBuyerMaster = new RootMenuEntity("RM_BUYER_MASTER", "Buyer Master",
				BusinessCodes.MENU_TYPE_SUPPLIER, 1L);
		RootMenuEntity rootMenuQuoteTemplateMaster = new RootMenuEntity("RM_QUOTE_TEMPLATE_MASTER",
				"Quote Template Maintenance", BusinessCodes.MENU_TYPE_SUPPLIER, 2L);
		RootMenuEntity rootMenuQuoteMaintenanceSupplier = new RootMenuEntity(
				"RM_SUPPLIER_QUOTE_MAINTENANCE", "Quote Maintenance", BusinessCodes.MENU_TYPE_SUPPLIER,
				3L);
		// Buyer Root Menus
		RootMenuEntity rootMenuQuoteMaintenanceBuyer = new RootMenuEntity("RM_BUYER_QUOTE_MAINTENANCE",
				"Quote Maintenance", BusinessCodes.MENU_TYPE_BUYER, 0L);
		session.save(rootMenuSupplierMaster);
		session.save(rootMenuSuperUserMaster);
		session.save(rootMenuBuyerMaster);
		session.save(rootMenuQuoteTemplateMaster);
		session.save(rootMenuQuoteMaintenanceSupplier);
		session.save(rootMenuQuoteMaintenanceBuyer);

		// Sub Menus
		// 1. Supplier Master Sub menus
		SubMenuEntity subMenuAddNewSupplier = new SubMenuEntity(rootMenuSupplierMaster,
				"SM_ADD_SUPPLIER", "Adds a New Supplier", 0L, "/supplier/addSupplier.xhtml",
				"/assets/images/supplier24.png");
		SubMenuEntity subMenuSupplierInquiry = new SubMenuEntity(rootMenuSupplierMaster,
				"SM_SUPPLIER_INQUIRY", "Supplier's Inquiry", 1L, "/supplier/listSuppliers.xhtml",
				"/assets/images/supplier24.png");
		SubMenuEntity subMenuSupplierUserMaintenance = new SubMenuEntity(rootMenuSupplierMaster,
				"SM_SUPPLIER_USER_MAINTENANCE", "User's Maintenance", 2L,
				"/supplier/listSuppliersForUserMaintenance.xhtml", "/assets/images/users24.png");
		session.save(subMenuAddNewSupplier);
		session.save(subMenuSupplierInquiry);
		session.save(subMenuSupplierUserMaintenance);

		// Super User Maintenance
		SubMenuEntity subMenuListSuperUsers = new SubMenuEntity(rootMenuSuperUserMaster,
				"SM_SUPER_USER_INQUIRY", "Super Users Maintenance", 0L, "/system/listSystemUsers.xhtml",
				"/assets/images/users24.png");
		session.save(subMenuListSuperUsers);
		
		// 2. Buyer Master Sub Menus
		SubMenuEntity subMenuAddNewBuyer = new SubMenuEntity(rootMenuBuyerMaster, "SM_ADD_BUYER",
				"Adds a New Buyer", 0L, "/buyer/addBuyer.xhtml", "/assets/images/supplier24.png");
		SubMenuEntity subMenuBuyerInquiry = new SubMenuEntity(rootMenuBuyerMaster, "SM_BUYER_INQUIRY",
				"Buyer Inquiry", 1L, "/buyer/listBuyers.xhtml", "/assets/images/supplier24.png");
		SubMenuEntity subMenuBuyerUserMaintenance = new SubMenuEntity(rootMenuBuyerMaster,
				"SM_BUYER_USER_MAINTENANCE", "User's Maintenance", 2L,
				"/buyer/listBuyersForUserMaintenance.xhtml", "/assets/images/users24.png");
		session.save(subMenuAddNewBuyer);
		session.save(subMenuBuyerInquiry);
		session.save(subMenuBuyerUserMaintenance);

		// 3. Quote Template Master Sub Menus
		SubMenuEntity subMenuAddNewQuoteTemplate = new SubMenuEntity(rootMenuQuoteTemplateMaster,
				"SM_ADD_QUOTE_TEMPLATE", "Add Quote Template", 0L, "/supplier/addQuoteTemplate.xhtml",
				"/assets/images/quote_template16.png");
		SubMenuEntity subMenuQuoteTemplateInquiry = new SubMenuEntity(rootMenuQuoteTemplateMaster,
				"SM_QUOTE_TEMPLATE_INQUIRY", "Add Quote Template", 1L,
				"/supplier/listSupplierQuoteTemplateList.xhtml", "/assets/images/quote_template16.png");
		session.save(subMenuAddNewQuoteTemplate);
		session.save(subMenuQuoteTemplateInquiry);

		// 4. Supplier Quote Transaction Maintenance
		SubMenuEntity subMenuSendQuoteToBuyer = new SubMenuEntity(rootMenuQuoteMaintenanceSupplier,
				"SM_SEND_QUOTE_TO_BUYER", "Send A Quote To Buyer", 0L,
				"/supplier/sendQuoteToBuyer.xhtml", "/assets/images/quote_template16.png");
		SubMenuEntity subMenuSendQuoteInquiry = new SubMenuEntity(rootMenuQuoteMaintenanceSupplier,
				"SM_SEND_QUOTE_INQUIRY", "Send A Quote To Buyer", 1L,
				"/supplier/listSentQuotesToBuyer.xhtml", "/assets/images/quote_template16.png");
		session.save(subMenuSendQuoteToBuyer);
		session.save(subMenuSendQuoteInquiry);

		// 5. Pending Quotes to Reply By Buyer
		SubMenuEntity subMenuBuyerPendingQuotesToReplyInquiry = new SubMenuEntity(
				rootMenuQuoteMaintenanceBuyer, "SM_PENDING_QUOTES_TO_REPLY", "Pending Quotes to Reply",
				0L, "/buyer/listBuyerPendingQuotesToReply.xhtml", "/assets/images/quote_template16.png");
		session.save(subMenuBuyerPendingQuotesToReplyInquiry);

		// 6. Pending Quotes to Reply By Buyer
		SubMenuEntity subMenuListAcceptedRejectedQuotesByBuyerInquiry = new SubMenuEntity(
				rootMenuQuoteMaintenanceBuyer, "SM_BUYER_QUOTES_INQUIRY",
				"List of Quotes Accepted or Ignored By Buyer", 1L, "/buyer/listBuyerQuotes.xhtml",
				"/assets/images/quote_template16.png");
		session.save(subMenuListAcceptedRejectedQuotesByBuyerInquiry);
	}

	/**
	 * @param session
	 */
	private static void removeExistingMenus(Session session)
	{
		// Remove all Sub Menu Items
		Criteria criteria = session.createCriteria(SubMenuEntity.class);
		List<SubMenuEntity> subMenuEntityList = criteria.list();
		for (SubMenuEntity subMenuEntity : subMenuEntityList)
		{
			session.delete(subMenuEntity);
		}
		// Remove all root Menus
		criteria = session.createCriteria(RootMenuEntity.class);
		List<RootMenuEntity> rootMenuEntityList = criteria.list();
		for (RootMenuEntity rootMenuEntity : rootMenuEntityList)
		{
			session.delete(rootMenuEntity);
		}
	}

}
